<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title></title>
		<link rel="stylesheet" type="text/css" href="css/1.css" />
	</head>

	<body>
		<!--外边框-->
		<div id="outer">
			<!--计算器标题和窗口按钮-->
			<div id="header">
				<div id="h1">
					计算器
				</div>
				<div id="h2">

					<input type="button" class="b1" value="-" />
					<input type="button" class="b1" value="口" />
					<input type="button" class="b1" value="X" />
				</div>
			</div>
			<!--选择计算器功能和历史记录菜单-->
			<div id="menu">
				<div>
					菜单
				</div>
				<div>
					标准
				</div>
				<div>
					历史记录
				</div>
			</div>
			<div id="content">
				<div id="display">
					<div id="his">

					</div>

					<div id="input">

					</div>
				</div>
				<div id="fms">
					<div class="ms">
						MC
					</div>
					<div class="ms">
						MR
					</div>
					<div class="ms">
						M+
					</div>
					<div class="ms">
						M-
					</div>
					<div class="ms">
						MS
					</div>
					<div class="ms">
						M
					</div>
				</div>
				<div id="but">

					<div class="key keyg">
						%
					</div>
					<div class="key keyg">
						√
					</div>
					<div class="key keyg" id="sqr">
						x<sup>2</sup>
					</div>
					<div class="key keyg">
						1/x
					</div>
					<div class="key keyg" id="cebut">
						CE
					</div>
					<div class="key keyg" id="cbut">
						C
					</div>
					<div class="key keyg" id="bsbut">
						&larr;
					</div>
					<div class="key keyg" id="div">
						&divide;
					</div>
					<div class="key keyw" onclick="kick('7')">
						7
					</div>
					<div class="key keyw" onclick="kick('8')">
						8
					</div>
					<div class="key keyw" onclick="kick('9')">
						9
					</div>
					<div class="key keyg" onclick="kick('*')">
						&times;
					</div>
					<div class="key keyw" onclick="kick('4')">
						4
					</div>
					<div class="key keyw" onclick="kick('5')">
						5
					</div>
					<div class="key keyw" onclick="kick('6')">
						6
					</div>
					<div class="key keyg" onclick="kick('-')">
						-
					</div>
					<div class="key keyw" onclick="kick('1')">
						1
					</div>
					<div class="key keyw" onclick="kick('2')">
						2
					</div>
					<div class="key keyw" onclick="kick('3')">
						3
					</div>
					<div class="key keyg" onclick="kick('+')">
						+
					</div>
					<div class="key keyg" id="neg">
						&plusmn;
					</div>
					<div class="key keyw" onclick="kick('0')">
						0
					</div>
					<div class="key keyg" onclick="kick('.')">
						.
					</div>
					<div class="key keyg" onclick="kick('=')">
						=
					</div>

				</div>
			</div>
		</div>

		<script type="text/javascript">
			var showInput = document.getElementById("input");
			var isClear = false;
			var tempStr = "";
			var clacType = "";
			var isContinue = true;

			function kick(clickValue) {
				switch(clickValue) {
					case "=":
						if(tempStr != "" && clacType != "") {
							showInput.value = clac(tempStr, showInput.innerText, clacType);
							isContinue = false;
							clacType = "";
						}
						break;
					case "+":
					case "-":
					case "*":
					case "/":
						//如果预存的操作符不为空 表示表示连续操作
						if(clacType != "" && !isContinue) { //先执行计算
							tempStr = clac(tempStr, showInput.innerText, clacType);
							isClear = true;
							clacType = clickValue;
						} else {
							tempStr = showInput.innerText; //点击操作符之后 预存字符
							isClear = true; //表示点击了操作符
							clacType = clickValue; //预存操作符
						}
						isContinue = true;
						break;
					case "C":
						showInput.value = "0";
						isClear = false;
						tempStr = "";
						clacType = "";
						break;
					default: //普通的数字按钮点击
						showInput.innerText = showInput.innerText == "0" ? "" : showInput.innerText;
						isContinue = false;
						if(isClear) {
							showInput.innerText = "";
							showInput.innerText += clickValue;
							isClear = false;
						} else {
							showInput.innerText += clickValue;
						}
						break;
				}
			}

			function clac(num1, num2, type) {
				switch(type) {
					case "+":
						return Number(num1) + Number(num2);
					case "-":
						return Number(num1) - Number(num2);
					case "*":
						return Number(num1) * Number(num2);
					case "/":
						return Number(num1) / Number(num2);
					default:
						break;
				}
			}

//			//CE:输入框清零
//			cebut.onclick = function() {
//				result.innerText = "0";
//				valueinput = "";
//				operator = 0;
//			}
//
//			//退格
//			bsbut.onclick = function() {
//				if(valueinput.length > 1) {
//					valueinput = valueinput.substring(0, valueinput.length - 1);
//					result.innerText = valueinput;
//				} else {
//					valueinput = "";
//					result.innerText = 0;
//				}
//			}
		</script>
	</body>

</html>